package com.example.mr5.money.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import com.example.mr5.money.constant.Constant;


/**
 * Created by Mr.5
 * Date：2016/9/11.
 * Time：22:35
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private String sql;
    private static DatabaseHelper databaseHelper;

    public static DatabaseHelper getDBInstance(Context context) {
        if (databaseHelper == null) {
            return new DatabaseHelper(context);
        } else {
            return databaseHelper;
        }
    }

    public DatabaseHelper(Context context) {
        super(context, Constant.DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create table

        //--------------------------------------------------------------------
        //income first table
        sql = "create table  "
                + Constant.DATABASE_INCOME_FIRST_TABLE_NAME + "("
                + Constant.DATABASE_INCOME_FIRST_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_INCOME_FIRST_TABLE_FIELD_TITLE + "  text )";
        db.execSQL(sql);
        ContentValues values = new ContentValues();
        values.put(Constant.DATABASE_INCOME_FIRST_TABLE_FIELD_TITLE, "职业收入");
        db.insert(Constant.DATABASE_INCOME_FIRST_TABLE_NAME, null, values);
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        //income second table
        sql = "create table  "
                + Constant.DATABASE_INCOME_SECOND_TABLE_NAME + "("
                + Constant.DATABASE_INCOME_SECOND_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_INCOME_SECOND_TABLE_FIELD_TITLE + "  text,"
                + Constant.DATABASE_INCOME_SECOND_TABLE_FIELD_FIRST_TITLE + "  text )";
        db.execSQL(sql);

        values.clear();
        values.put(Constant.DATABASE_INCOME_SECOND_TABLE_FIELD_TITLE, "工资");
        values.put(Constant.DATABASE_INCOME_SECOND_TABLE_FIELD_FIRST_TITLE, "职业收入");
        db.insert(Constant.DATABASE_INCOME_SECOND_TABLE_NAME, null, values);

        //--------------------------------------------------------------------


        //--------------------------------------------------------------------
        //expense first table
        sql = "create table  "
                + Constant.DATABASE_EXPENSE_FIRST_TABLE_NAME + "("
                + Constant.DATABASE_EXPENSE_FIRST_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_EXPENSE_FIRST_TABLE_FIELD_TITLE + "  text )";
        db.execSQL(sql);
        values.clear();
        values.put(Constant.DATABASE_EXPENSE_FIRST_TABLE_FIELD_TITLE, "食");
        db.insert(Constant.DATABASE_EXPENSE_FIRST_TABLE_NAME, null, values);

        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        //expense second table
        sql = "create table  "
                + Constant.DATABASE_EXPENSE_SECOND_TABLE_NAME + "("
                + Constant.DATABASE_EXPENSE_SECOND_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_EXPENSE_SECOND_TABLE_FIELD_TITLE + "  text,"
                + Constant.DATABASE_EXPENSE_SECOND_TABLE_FIELD_FIRST_TITLE + "  text )";
        db.execSQL(sql);

        values.clear();
        values.put(Constant.DATABASE_EXPENSE_SECOND_TABLE_FIELD_FIRST_TITLE, "食");
        values.put(Constant.DATABASE_EXPENSE_SECOND_TABLE_FIELD_TITLE, "早餐");
        db.insert(Constant.DATABASE_EXPENSE_SECOND_TABLE_NAME, null, values);
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        sql = "create table  "
                + Constant.DATABASE_INCOME_TABLE_NAME + "("
                + Constant.DATABASE_INCOME_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_INCOME_TABLE_FIELD_HOW_MUCH + "  float,"
                + Constant.DATABASE_INCOME_TABLE_FIELD_DATE + "  text,"
                + Constant.DATABASE_INCOME_TABLE_FIELD_SUB_SOURCE + "  text,"
                + Constant.DATABASE_INCOME_TABLE_FIELD_MAIN_SOURCE + "  text )";
        db.execSQL(sql);
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        sql = "create table  "
                + Constant.DATABASE_EXPENSE_TABLE_NAME + "("
                + Constant.DATABASE_EXPENSE_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_EXPENSE_TABLE_FIELD_HOW_MUCH + "  float,"
                + Constant.DATABASE_EXPENSE_TABLE_FIELD_DATE + "  text,"
                + Constant.DATABASE_EXPENSE_TABLE_FIELD_SUB_SOURCE + "  text,"
                + Constant.DATABASE_EXPENSE_TABLE_FIELD_MAIN_SOURCE + "  text )";
        db.execSQL(sql);
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        sql = "create table  "
                + Constant.DATABASE_EXPRESS_RECORD_TABLE_NAME + "("
                + Constant.DATABASE_EXPRESS_RECORD_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_EXPRESS_RECORD_TABLE_FIELD_COMPANY + "  text,"
                + Constant.DATABASE_EXPRESS_RECORD_TABLE_FIELD_NUMBER + "  text )";
        db.execSQL(sql);
        //--------------------------------------------------------------------

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
